#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib,urllib2,re,os,xbmc

#author:
#elypter
#-mail:elypter@yahoo.de
#
#inspired by jdownloader-megavideo-hoster-plugin and the megavideo-player-actionscript-sourcecode
#
#for details look at default.py

#megavideo.com hoster plugin

debug=1
dstr=""

def check(url):
	if url.rfind("megavideo"):
		return 1
	else:
		return 0

def get_regexp():
	#return "(?:http\://www\.megavideo\.com/v/([A-Z]+)[0-9a-z]*|http\://www\.megavideo\.com/\?v\=([0-9a-z]*))"
	return "http\://www\.megavideo\.com/(?:\?v\=|v/)([0-9a-zA-Z]{8})"
def get_name():
	return "megavideo"
def join(id):
	return "http://www.megavideo.com/?v="+id
	
def get_link1(url):
	#takes an videopage-url of megavideo
	#
	#returnes the service/mirror name, the url that will be passed over to get_link2, the url of the verification code
	
	if debug > 0: print dstr+"*hoster*megavideo**url*: "+ url
	
	id=re.compile('http\://www\.megavideo\.com/v/([A-Z0-9a-z]{8})').findall(url)
	if id==None or len(id)<1:
		id=re.compile('http\://www\.megavideo\.com/\?v\=([A-Z0-9a-z]{8})').findall(url)
	
	print "|||||||||||||||"+str(id)
	
	if id == None or len(id)<1:
		return ("megavideo",'','')
	
	req = urllib2.Request("http://www.megavideo.com/xml/videolink.php?v="+id[0])
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
	response = urllib2.urlopen(req)
	page=response.read()
	response.close()
	
	#print page
	
	s = re.compile(' s\="([^<>"]*)"').findall(page)
	un = re.compile(' un\="([^<>"]*)"').findall(page)
	k1 = re.compile(' k1\="([^<>"]*)"').findall(page)
	k2 = re.compile(' k2\="([^<>"]*)"').findall(page)
	
	if debug > 0: print dstr+"*hoster*megavideo**parameter* "+ str(s)+"-- "+str(un)+"---- "+str(k1)+"----- "+str(k2)
	
	if (s == None or un == None or k1 == None or k2 == None or len(s)<1 or len(un)<1 or len(k1)<1 or len(k2)<1):
		return ("megavideo",'','')
	out = "http://www" + s[0] + ".megavideo.com/files/" + decrypt(un[0], int(k1[0]), int(k2[0])) + "/"
	
	#return ("megavideo",out,'')
	
	#return #status,link,image,title,filename,captcha,hoster	
	return ("got",out,'','','','','megavideo')
	
def get_link2(url1,code):
	#should take the url that get_link1 returned
	#
	#returnes the video url
	return ("got",url1,'','','','','megavideo')
	
def decrypt(input, k1, k2):
		req1 = []
		req3 = 0
		while req3 < len(input):
			c = input[req3]
			if c=='-1':
				print "nothing"
			elif c=='0':
				req1.append(0);
				req1.append(0);
				req1.append(0);
				req1.append(0);
			elif c=='1':
				req1.append(0);
				req1.append(0);
				req1.append(0);
				req1.append(1);
			elif c=='2':
				req1.append(0);
				req1.append(0);
				req1.append(1);
				req1.append(0);
			elif c=='3':
				req1.append(0);
				req1.append(0);
				req1.append(1);
				req1.append(1);
			elif c=='4':
				req1.append(0);
				req1.append(1);
				req1.append(0);
				req1.append(0);
			elif c=='5':
				req1.append(0);
				req1.append(1);
				req1.append(0);
				req1.append(1);
			elif c=='6':
				req1.append(0);
				req1.append(1);
				req1.append(1);
				req1.append(0);
			elif c=='7':
				req1.append(0);
				req1.append(1);
				req1.append(1);
				req1.append(1);
			elif c=='8':
				req1.append(1);
				req1.append(0);
				req1.append(0);
				req1.append(0);
			elif c=='9':
				req1.append(1);
				req1.append(0);
				req1.append(0);
				req1.append(1);
			elif c=='a':
				req1.append(1);
				req1.append(0);
				req1.append(1);
				req1.append(0);
			elif c=='b':
				req1.append(1);
				req1.append(0);
				req1.append(1);
				req1.append(1);
			elif c=='c':
				req1.append(1);
				req1.append(1);
				req1.append(0);
				req1.append(0);
			elif c=='d':
				req1.append(1);
				req1.append(1);
				req1.append(0);
				req1.append(1);
			elif c=='e':
				req1.append(1);
				req1.append(1);
				req1.append(1);
				req1.append(0);
			elif c=='f':
				req1.append(1);
				req1.append(1);
				req1.append(1);
				req1.append(1);
			req3=req3+1
		

		req6 = []
		req3 = 0
		while req3 < 384:
			k1 = (k1 * 11 + 77213) % 81371
			k2 = (k2 * 17 + 92717) % 192811
			req6.append((k1 + k2) % 128)
			req3=req3+1;
			
		req3 = 256
		while req3 >= 0:
			req5 = req6[req3]
			req4 = req3 % 128
			req8 = req1[req5]
			req1[req5] = req1[req4]
			req1[req4] = req8
			req3=req3-1

		req3 = 0;
		while req3 < 128:
			req1[req3] = (req1[req3]) ^ (req6[req3 + 256] & 1)
			req3=req3+1


		out = ""
		req3 = 0
		while req3 < len(req1):
			tmp = (req1[req3] * 8);
			tmp += (req1[req3 + 1] * 4);
			tmp += (req1[req3 + 2] * 2);
			tmp += (req1[req3 + 3]);
			
			if tmp==-1:
				print "nothing"
			elif tmp==0:
				out = out + "0";
			elif tmp==1:
				out = out + "1";
			elif tmp==2:
				out = out + "2";
			elif tmp==3:
				out = out + "3";
			elif tmp==4:
				out = out + "4";
			elif tmp==5:
				out = out + "5";
			elif tmp==6:
				out = out + "6";
			elif tmp==7:
				out = out + "7";
			elif tmp==8:
				out = out + "8";
			elif tmp==9:
				out = out + "9";
			elif tmp==10:
				out = out + "a";
			elif tmp==11:
				out = out + "b";
			elif tmp==12:
				out = out + "c";
			elif tmp==13:
				out = out + "d";
			elif tmp==14:
				out = out + "e";
			elif tmp==15:
				out = out + "f";
			req3 += 4;
		return out;